package cn.ak47007.springboot.controller;

import cn.ak47007.springboot.util.QiNiuYunUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * @Author AK47007
 * @Date 2019/6/5 14:41
 * Describe:
 */
@RequestMapping
@RestController
public class Controller {

    @Autowired
    private QiNiuYunUtil qiNiuYunUtil;


//    @RequestMapping("")
//    public String index() {
//        return "index03";
//    }

    @RequestMapping("/uploadPictures")
    @ResponseBody
    public String uploadPictures(@RequestParam("files") MultipartFile[] files) {

        System.out.println(files);
        System.out.println("进来了");

        if (files != null && files.length > 0) {
            for (int i = 0; i < files.length; i++) {
                try {
                    FileInputStream fileInputStream = (FileInputStream) files[i].getInputStream();

                    String url = qiNiuYunUtil.uploadQiniuYun(fileInputStream);
                    System.out.println(url);
                    //输出url上传后的,可以复制url到浏览器访问
                    System.out.println("url=" + url);
                    try {
                        //延迟两秒让七牛云缓一下
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("success");
        //上传图片成功
        return "success";
    }

}
